import dayjs from 'dayjs'

Page({
  data: {
    now: dayjs(new Date()).format('YYYY年M月D日'),
    records: [],
    add: 0,
    minus: 0
  },
  onShow() {
    this.setData({
      records: wx.getStorageSync('recordList') || []
    })
    this.getTotal()
  },
  getTotal() {
    let {
      add,
      minus
    } = this.data
    const {
      records
    } = this.data
    const addRecords = records.filter(i => i.type === '+')
    const minusRecords = records.filter(i => i.type === '-')
    add = addRecords.reduce((sum, item) => {
      return sum + item.amount
    }, 0)
    minus = minusRecords.reduce((sum, item) => {
      return sum + item.amount
    }, 0)
    this.setData({
      add,
      minus
    })
  }
})